package org.krtrain.sports.tracker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.krtrain.sports.team.Team;
import org.krtrain.sports.venue.Venue;
import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;

/**
 * @author Kuali Rice Team (rice.collab@kuali.org)
 */
public class Game extends PersistableBusinessObjectBase {
    private static final long serialVersionUID = 759818029447609052L;

    private String gameId;

    private String homeTeamName;
    private String visitingTeamName;
    private Date date;
    private String venueId;

    private int minutesRemaining = 20;
    private int secondsRemaining = 0;
    private int half = 1;
    private int homePoints;
    private int visitingPoints;

    private Team homeTeam;
    private Team visitingTeam;
    private Venue venue;

    private List<PlayerStatistics> playerStats;
    private List<GamePlay> gamePlays;

    public Game() {
        super();

        playerStats = new ArrayList<PlayerStatistics>();
        gamePlays = new ArrayList<GamePlay>();
    }

    public String getGameId() {
        return gameId;
    }

    public void setGameId(String gameId) {
        this.gameId = gameId;
    }

    public String getHomeTeamName() {
        return homeTeamName;
    }

    public void setHomeTeamName(String homeTeamName) {
        this.homeTeamName = homeTeamName;
    }

    public String getVisitingTeamName() {
        return visitingTeamName;
    }

    public void setVisitingTeamName(String visitingTeamName) {
        this.visitingTeamName = visitingTeamName;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getVenueId() {
        return venueId;
    }

    public void setVenueId(String venueId) {
        this.venueId = venueId;
    }

    public Venue getVenue() {
        return venue;
    }

    public void setVenue(Venue venue) {
        this.venue = venue;
    }

    public int getMinutesRemaining() {
        return minutesRemaining;
    }

    public void setMinutesRemaining(int minutesRemaining) {
        this.minutesRemaining = minutesRemaining;
    }

    public int getSecondsRemaining() {
        return secondsRemaining;
    }

    public void setSecondsRemaining(int secondsRemaining) {
        this.secondsRemaining = secondsRemaining;
    }

    public int getHalf() {
        return half;
    }

    public void setHalf(int half) {
        this.half = half;
    }

    public int getHomePoints() {
        return homePoints;
    }

    public void setHomePoints(int homePoints) {
        this.homePoints = homePoints;
    }

    public int getVisitingPoints() {
        return visitingPoints;
    }

    public void setVisitingPoints(int visitingPoints) {
        this.visitingPoints = visitingPoints;
    }

    public Team getHomeTeam() {
        return homeTeam;
    }

    public void setHomeTeam(Team homeTeam) {
        this.homeTeam = homeTeam;
    }

    public Team getVisitingTeam() {
        return visitingTeam;
    }

    public void setVisitingTeam(Team visitingTeam) {
        this.visitingTeam = visitingTeam;
    }

    public List<PlayerStatistics> getPlayerStats() {
        return playerStats;
    }

    public void setPlayerStats(List<PlayerStatistics> playerStats) {
        this.playerStats = playerStats;
    }

    public List<GamePlay> getGamePlays() {
        return gamePlays;
    }

    public void setGamePlays(List<GamePlay> gamePlays) {
        this.gamePlays = gamePlays;
    }

    public List<GamePlay> getLatestPlays(){
        List<GamePlay> latest = new ArrayList<GamePlay>(gamePlays);
        Collections.sort(latest, new Comparator<GamePlay>() {
            @Override
            public int compare(GamePlay gp1, GamePlay gp2) {
                int returnVal = 0;
                if(gp1.getHalf() > gp2.getHalf()){
                    return 1;
                }
                else if(gp1.getHalf() < gp2.getHalf()){
                    return -1;
                }
                //must be equal half beyond this point
                else if(gp1.getMinutesRemaining() < gp2.getMinutesRemaining()){
                    return 1;
                }
                else if(gp1.getMinutesRemaining() > gp2.getMinutesRemaining()){
                    return -1;
                }
                //must be equal minutes beyond this point
                else if(gp1.getSecondsRemaining() < gp2.getMinutesRemaining()){
                    return 1;
                }
                else if(gp1.getSecondsRemaining() > gp2.getMinutesRemaining()){
                    return -1;
                }
                else{
                    //play occured at the same time
                    return 0;
                }
            }
        });
        return latest;
    }
}
